package com.lq.common.until;

import com.google.gson.Gson;
import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;

/**
 * @author 权哥
 * @version 1.0
 * @date 2023/7/21 21:33
 */

public class Pages {

    /**
     * 获取短文内容
     * @param pageUrlName
     * @return
     */
    public Map<String,String>   getPage(String pageUrlName){

        String requestUrl = "https://apis.tianapi.com/"+pageUrlName+"/index?key=5f300375cf73525f57742c7dda5563b3";

        Map<String,String> pageMap = new HashMap<>();
        try {
            // 创建 URL 对象
            URL url = new URL(requestUrl);

            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod("GET");

            // 发送请求并获取响应状态码
            int responseCode = connection.getResponseCode();

            // 判断请求是否成功
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取响应数据
                BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
                String line;
                StringBuilder response = new StringBuilder();
                while ((line = reader.readLine()) != null) {
                    response.append(line);
                }
                reader.close();

                // 解析响应数据
                Gson gson = new Gson();
                JsonObject object = gson.fromJson(response.toString(),JsonObject.class);

                String msgStr = object.get("msg").toString();
                String msg = msgStr.substring(1,msgStr.length()-1);
                String contentObject = "";

                if(msg.equals("success")){
                    contentObject = gson.fromJson(object.get("result"),JsonObject.class).get("content").toString();
                    pageMap.put("content",contentObject.substring(1,contentObject.length()-1)); //短语获取内容
                }

                pageMap.put("msg",msg); //短语获取状态
                pageMap.put("requestUrl",requestUrl);

            } else {
                System.out.println("接口请求错误");
            }

            // 关闭连接
            connection.disconnect();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return pageMap;
    }


    public static String api(String requestUrl,String requestType){
        try {
            // 创建 URL 对象
            URL url = new URL(requestUrl);

            // 创建 HttpURLConnection 对象
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            // 设置请求方法为 GET
            connection.setRequestMethod(requestType);

            // 获取响应状态码
            int responseCode = connection.getResponseCode();

            // 读取响应内容
            BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            String line;
            StringBuilder response = new StringBuilder();
            while ((line = reader.readLine()) != null) {
                response.append(line);
            }
            reader.close();

            // 关闭连接
            connection.disconnect();

            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}